package com.wuyu.web.controller;

import com.wuyu.base.BaseController;
import com.wuyu.base.Result;
import com.wuyu.constants.BaseEnums;
import com.wuyu.domain.User;
import com.wuyu.recDomain.UserRecDomain;
import com.wuyu.service.UserService;
import com.wuyu.utils.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户Controller
 *
 * @version 1.0
 * @author wuyu 2019-05-22
 */
@Api(tags="用户操作接口",value="用户Controller")
@RequestMapping("")
@RestController
public class UserController extends BaseController {

    private static final String INDEX = "index";
    @Autowired
    private UserService userService;
    
    private static Logger logger = LoggerFactory.getLogger(UserService.class);
        
    @ApiOperation("登录")
    @RequestMapping("/submit/login")
    public String login(@ModelAttribute @ApiParam(name="用户对象",value="传入json格式",required=true) UserRecDomain userRecDomain) {
        
        Result result = null;
        
        String loginName = userRecDomain.getLoginName();
        String password = userRecDomain.getPassword();
        //判空登录名和密码
        if(null == loginName || "".equals(loginName))            
            result = Results.failureWithData(false, BaseEnums.FAILURE.code(), "登录名不能为空");
        else if(null == password || "".equals(password))
            result = Results.failureWithData(false, BaseEnums.FAILURE.code(), "密码不能为空");
        else {
            User user = userService.selectUserByCondition(userRecDomain);
            
            if(null == user)
                result = Results.failureWithData(false, BaseEnums.FAILURE.code(), "帐号或密码错误，请重新输入！");
            else            
                result = Results.successWithData(user, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
        }
        
        logger.debug(JSONObject.fromObject(result).toString());
        return JSONObject.fromObject(result).toString();
    }
    
    @ApiOperation("注册")
    @RequestMapping("/submit/register")
    public Result register(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) UserRecDomain userRecDomain) {
        
        
        String loginName = userRecDomain.getLoginName();
        String password = userRecDomain.getPassword();
        String email = userRecDomain.getEmail();
        //判空登录名和密码
        if(null == loginName || "".equals(loginName))
            return Results.failureWithData(false, BaseEnums.FAILURE.code(), "登录名不能为空");
        if(null == password || "".equals(password))
            return Results.failureWithData(false, BaseEnums.FAILURE.code(), "密码不能为空");
        if(null == email || "".equals(email))
            return Results.failureWithData(false, BaseEnums.FAILURE.code(), "邮箱不能为空");
        
        UserRecDomain domain = new UserRecDomain();
        domain.setLoginName(loginName);
        User user = userService.selectUserByCondition(domain);
        
        if(null != user)
            return Results.failureWithData(false, BaseEnums.FAILURE.code(), "账号已存在，请重新输入！");
        else
        {
            user = new User();
            BeanUtils.copyProperties(userRecDomain , user);
            user = userService.insertSelective(user);           
            return Results.successWithData(user, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
        }
            
    }
    
    @ApiOperation("忘记密码")
    @RequestMapping("/submit/forgetPassword")
    public Result forgetPassword(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) UserRecDomain userRecDomain) {
        
        
        String loginName = userRecDomain.getLoginName();
        String password = userRecDomain.getPassword();
        String email = userRecDomain.getEmail();
        //判空登录名和密码
        if(null == loginName || "".equals(loginName))
            return Results.failureWithData(false, BaseEnums.FAILURE.code(), "登录名不能为空");
        if(null == password || "".equals(password))
            return Results.failureWithData(false, BaseEnums.FAILURE.code(), "密码不能为空");
        if(null == email || "".equals(email))
            return Results.failureWithData(false, BaseEnums.FAILURE.code(), "邮箱不能为空");
        
        UserRecDomain domain = new UserRecDomain();
        domain.setLoginName(loginName);
        User user = userService.selectUserByCondition(domain);
        
        if(null != user)
            return Results.failureWithData(false, BaseEnums.FAILURE.code(), "账号已存在，请重新输入！");
        else
        {
            user = new User();
            BeanUtils.copyProperties(userRecDomain , user);
            user = userService.insertSelective(user);           
            return Results.successWithData(user, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
        }           
    }
    
    
    
    
    
    
    
    @ApiOperation("输出index")
    @RequestMapping("/show")
    public String getIndex() {
        logger.info("--------------------index-------------------");
        return INDEX;
    }


    @ApiOperation("查询全部queryAll")
    @RequestMapping("/queryAll")
    public String queryAll() {
        List<User> list = userService.selectAll();
        return JSONObject.fromObject(Results.successWithData(list, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc())).toString();
    }
    
    @ApiOperation("根据id查询queryOne")
    @ApiImplicitParam(name = "userId", value = "用户编号", required=true,defaultValue = "80")//,dataType ="Long"
    @RequestMapping("/queryOne/{userId}")
    public Result queryOne(@PathVariable Long userId){
        User user = userService.get(userId);
        return Results.successWithData(user);
    }

    @ApiOperation("保存save")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody User user){
        user = userService.insertSelective(user);
        return Results.successWithData(user);
    }

    @ApiOperation("更新update")
    @PostMapping("/update")
    public Result update(@Valid @RequestBody List<User> user){
        user = userService.persistSelective(user);
        return Results.successWithData(user);
    }

/*    @RequestMapping("/delete")
    public Result delete(User user){
        userService.delete(user);
        return Results.success();
    }

    @RequestMapping("/{userId}")
    public Result delete(@PathVariable Long userId){
        userService.delete(userId);
        return Results.success();
    }*/

}